package cn.chiship.bs.api.biz.controller;

import cn.chiship.bs.api.biz.entity.ProductCategory;
import cn.chiship.bs.api.biz.entity.ProductCategoryExample;
import cn.chiship.bs.api.biz.entity.UpmsRole;
import cn.chiship.bs.api.biz.entity.UpmsRoleExample;
import cn.chiship.bs.api.biz.pojo.dto.ProductCategoryDto;
import cn.chiship.bs.api.biz.pojo.dto.UpmsRoleDto;
import cn.chiship.bs.api.biz.service.BaseService;
import cn.chiship.bs.api.biz.service.ProductCategoryService;
import cn.chiship.bs.api.biz.service.UpmsRoleService;
import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类控制器
 *
 * @author lj
 */
@RestController
@Api(tags = "商品分类接口")
@RequestMapping("/productCategory")
public class ProductCategoryController extends BaseController<ProductCategory, ProductCategoryExample> {
    @Resource
    ProductCategoryService productCategoryService;

    @Override
    public BaseService<ProductCategory, ProductCategoryExample> getService() {
        return productCategoryService;
    }

    @ApiOperation(value = "商品分类分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataTypeClass = String.class, paramType = "query"),
    })
    @Authorization
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(
            @RequestParam(required = false, value = "keyword") String keyword) {
        ProductCategoryExample productCategoryExample = new ProductCategoryExample();
        ProductCategoryExample.Criteria criteria = productCategoryExample.createCriteria();
        if (!StringUtil.isNullOrEmpty(keyword)) {
            criteria.andNameLike(keyword + "%");
        }
        return super.responseEntity(BaseResult.ok(super.page(productCategoryExample)));
    }

    @ApiOperation(value = "商品分类列表")
    @ApiImplicitParams({
    })
    @Authorization
    @GetMapping(value = "/list")
    public ResponseEntity<BaseResult> list() {
        ProductCategoryExample productCategoryExample = new ProductCategoryExample();
        productCategoryExample.setOrderByClause("orders asc");
        return super.responseEntity(BaseResult.ok(super.list(productCategoryExample)));
    }


    @ApiOperation(value = "保存商品分类")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid ProductCategoryDto productCategoryDto) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryDto, productCategory);
        return super.responseEntity(super.save(productCategory));
    }

    @ApiOperation(value = "更新商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品分类id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(
            @PathVariable("id") String id,
            @RequestBody @Valid ProductCategoryDto productCategoryDto) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryDto, productCategory);
        return super.responseEntity(super.update(id, productCategory));
    }

    @ApiOperation(value = "删除商品分类数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键ID集合,多个ID,请用,分割", required = true, dataType = "List", paramType = "body")
    })
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody List<String> ids) {

        ProductCategoryExample productCategoryExample = new ProductCategoryExample();
        productCategoryExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(productCategoryExample));

    }


}
